var url = "listPage.do";
var config = {page:1,pageSize:5};
Vue.filter("statusText", function(value){
    var status;
    if(value==0){
        status="未审核";
    }else if(value==1){
        status ="已启用";
    }else if(value==2){
        status ="已停用";
    }
    return status;
})

Vue.filter("date", function(time) {
    var now = new Date(time);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
})


layui.use(['table','form', 'layedit', 'laydate','jquery','laypage'], function () {
    var table = layui.table
        ,form = layui.form
        ,F = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate
        ,$ = layui.jquery
        ,laypage = layui.laypage;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        console.log(obj)
    });
    //监听工具条
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            layer.msg('ID：' + data.id + ' 的查看操作');
        } else if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.alert('编辑行：<br>' + JSON.stringify(data))
        }
    });

    var $ = layui.$, active = {
        getCheckData: function () { //获取选中数据
            var checkStatus = table.checkStatus('idTest')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
        , getCheckLength: function () { //获取选中数目
            var checkStatus = table.checkStatus('idTest')
                , data = checkStatus.data;
            layer.msg('选中了：' + data.length + ' 个');
        }
        , isAll: function () { //验证是否全选
            var checkStatus = table.checkStatus('idTest');
            layer.msg(checkStatus.isAll ? '全选' : '未全选')
        }
    };

    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    $('#add').on('click',function () {
        layer.open({
            type: 2,
            title: ['新建模板', 'font-size:13px;margin-top:10px;text-align:center'],
            content: ctx+'/template/addTemplatePage.do',//con是Ajax返回的页面
            // btn: ['确定', '取消'],
            area: ['400px', '350px'],
            shade: false,
            maxmin: true,
            anim: 2,

        })
    });

    var template = new Vue({
        el: '#template-list',
        data: {
            list: [],
        },
        methods: {
            shenghe:function (template) {
                $.ajax({
                    type: "post",
                    url: ctx+"/template/passStatus.do",
                    contentType: "application/json",
                    data:JSON.stringify(template),
                    async: false,
                    success: function (res) {
                        console.log(res);
                        if (res.resultCode == 10000) {
                            layer.msg(res.msg, {icon: 1});
                            getPageList();
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                });
            },
            tingyong:function (template) {
                $.ajax({
                    type: "post",
                    url: ctx+"/template/stopStatus.do",
                    contentType: "application/json",
                    data:JSON.stringify(template),
                    async: false,
                    success: function (res) {
                        console.log(res);
                        if (res.resultCode == 10000) {
                            layer.msg(res.msg, {icon: 1});
                                getPageList();
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                });
            }
        }
    })

    getPageList();
    function getPageList(){
        $.getJSON(url,config,function(res){
            laypage.render({
                elem: 'page',
                count: res.total, //总条数
                limit:config.pageSize, //每页条数
                // theme: '#FFB800', //自定义颜色
                jump: function(obj, first){
                    if(!first){ //首次则不进入
                        config.page = obj.curr;
                        getUserListByPage(url,config);
                    }
                }
            });
            template.list = res.rows;
        });
    }


    //点击页数从后台获取数据
    function getUserListByPage(url,config){
        $.getJSON(url,config,function(res){
            template.list = res.rows;
        });
    }






});

